package com.xbb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbb.entity.User;
import com.xbb.entity.UserParam;
import com.xbb.service.UserService;
import com.xbb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     *用户多条件分页查询
     * @param userParam
     * @return
     */
    @PostMapping("/getUserList")
    public Object getUserList(UserParam userParam){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if (!StringUtils.isEmpty(userParam.getLoginName())) {
            queryWrapper.like("login_name", userParam.getLoginName());
        }
        if (!StringUtils.isEmpty(userParam.getBeginTime())) {
            queryWrapper.gt("creat_time", userParam.getBeginTime());
        }
        if (!StringUtils.isEmpty(userParam.getEndTime())) {
            queryWrapper.lt("creat_time", userParam.getEndTime());
        }
        if (!StringUtils.isEmpty(userParam.getIsLockout())) {
            queryWrapper.eq("is_lockout", userParam.getIsLockout());
        }
        if (!StringUtils.isEmpty(userParam.getOrderBy())) {
            queryWrapper.orderByAsc(userParam.getOrderBy());
        }
        Page<User> page = new  Page<User>(userParam.getPage(),userParam.getLimit());
        IPage<User> iPage = userService.page(page, queryWrapper);
        List<User> data = iPage.getRecords();
        long total = iPage.getTotal();
        return new Result(0,"用户列表",total,data);
    }

    /**
     *添加新用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Object addUser(@Valid User user){
        boolean save = userService.save(user);
        if(save) {
            return new Result(0,"添加成功");
        }else {
            return new Result(1,"添加失败");
        }
    }

    /**
     * 编辑基本信息
     * @param id
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public Object updateUser(@RequestParam Integer id,@Valid User user){
        user.setId(id);
        boolean update = userService.updateById(user);
        if(update) {
            return new Result(0,"修改成功");
        }else {
            return new Result(1,"修改失败");
        }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/delUser")
    public Object delUser(@RequestParam Integer id) {
        boolean remove = userService.removeById(id);
        if(remove) {
            return new Result(0,"删除成功");
        }else {
            return new Result(1,"删除失败");
        }
    }

    /**
     * 密码重置
     * @param id
     * @return
     */
    @PostMapping("/resetPsw")
    public Object resetPsw(@RequestParam Integer id) {
        User user = new User();
        user.setId(id);
        user.setPassword("123456");
        boolean res=userService.updateById(user);
        if(res) {
            return new Result(0,"重置密码成功");
        }else {
            return new Result(1,"重置密码失败");
        }
    }

    /**
     * 用户锁定功能
     * @param id
     * @return
     */
    @PostMapping("/lock")
    public Object lock(@RequestParam Integer id) {
        User user=new User();
        user.setId(id);
        user.setIsLockout("是");
        boolean res=userService.updateById(user);
        if(res) {
            return new Result(0,"锁定成功");
        }else {
            return new Result(1,"锁定失败");
        }
    }

    /**
     * 用户解锁功能
     * @param id
     * @return
     */
    @PostMapping("/unlock")
    public Object unlock(@RequestParam Integer id) {
        User user=new User();
        user.setId(id);
        user.setIsLockout("否");
        boolean res=userService.updateById(user);
        if(res) {
            return new Result(0,"解锁成功");
        }else {
            return new Result(1,"解锁失败");
        }
    }
}
